/*
 * @file bucket.h
 */

#include <boost/algorithm/string.hpp>
#include <boost/functional/hash.hpp>

struct Bucket {
	int key;
	Bucket * prev,* next;
	ENTRY_P_SET elements;

	Bucket(int _key, Bucket *_prev, Bucket *_next ) {
		key = _key;
		prev = _prev;
		next = _next;
	}

	~Bucket() {}

	void add(Entry *i) {
		printf("add: has entry: %d\n", (this->elements.find(i) != this->elements.end()));
		this->elements.insert(i);
		printf("My #el : %d\n", elements.size());
	}

	void remove(Entry *i ) {
		ENTRY_P_SET::iterator it = elements.find(i);
		printf("remove: has entry: %d\n", (it != this->elements.end()));
		this->elements.erase(*it);
	}

	bool has_elements() {
		return this->elements.size() > 0;
	}


	bool operator<(const Bucket& rhs) {
			return key < rhs.key;
	}

	bool operator==(const Bucket& rhs) {
		return key == rhs.key;
	}

	size_t hashvalue() const {
	    boost::hash<int> hasher;
	    return hasher(key);
	}

	string to_string() {
		stringstream ss;
		ENTRY_P_SET::iterator it = elements.begin();
		int _prev = prev != NULL ? (*prev).key : -1;
		int _next = next != NULL ? (*next).key : -1;
		ss << "bucket " << key << ": " << _prev << ":" << _next <<  ":";
		for (; it != elements.end(); it++) {
			ss << (*(*it)).to_string() << " ";
		}
		return ss.str();
	}
};

/*size_t hash_value(Bucket const&b) {
    boost::hash<int> hasher;
    return hasher(b.key);
}*/

